package cn.initcap.license.create.config;

import com.google.common.collect.Lists;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author initcap
 * @date Created in 2018/7/4 PM4:59.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket swaggerSpringMvcPluginLicenseCreate() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .groupName("license")
                .select()   // 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.basePackage("cn.initcap.license.create.controller"))
                //处理认证授权
                .paths(PathSelectors.regex("^(?!auth).*$"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /**
     * 添加认证授权按钮
     *
     * @return
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> keyList = Lists.newArrayList();
        ApiKey key = new ApiKey("Authorization", "Authorization", "header");
        keyList.add(key);
        return keyList;
    }

    /**
     * 授权内容
     *
     * @return
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> contextList = Lists.newArrayList();
        SecurityContext context = SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build();
        contextList.add(context);
        return contextList;
    }

    /**
     * 默认授权
     *
     * @return
     */
    List<SecurityReference> defaultAuth() {
        List<SecurityReference> referenceList = Lists.newArrayList();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        SecurityReference reference = new SecurityReference("Authorization", authorizationScopes);
        referenceList.add(reference);
        return referenceList;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("授权管理API")
                .termsOfServiceUrl("www.initcap.cn")
                .version("1.0").build();
    }
}
